package com.demo.http;

import java.util.concurrent.atomic.AtomicReference;

import io.netty.handler.codec.http.HttpMethod;
import reactor.netty.http.client.HttpClient;
import reactor.netty.resources.ConnectionProvider;

public class Client {

	public static void main(String[] args) {
		final AtomicReference<HttpMethod> method = new AtomicReference<>();
		final AtomicReference<String> path = new AtomicReference<>();

		ConnectionProvider pool = ConnectionProvider.create("abort", 2);
		HttpClient client = createHttpClientForContextWithPort(pool);

		client
			.doOnRequest((req, con) -> {
	            method.set(req.method());
	            path.set(req.path());
	        })
	        .get()
	        .uri("/hello")
	        .response((r, buf) -> {
	        	return buf.asString();
	        	//return Mono.just(r.status().code());
	        }).subscribe(status -> {
	        	System.out.println(status);
	        });

		try {
			Thread.sleep(1000 * 1000l);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}

	private static HttpClient createHttpClientForContextWithPort(ConnectionProvider pool) {
		HttpClient client;
		if (pool == null) {
			client = HttpClient.create();
		}
		else {
			client = HttpClient.create(pool);
		}
		return client.port(8083);
	}

}
